package testrank.affinity;

import java.io.File;
import java.io.FileNotFoundException;

import testrank.TestRankConstants;

/**
 * @author Hagai Cibulski
 *
 */
public class SourcePathUtil {

	//TODO use sourcepath environment variable or something
	
	public static String fix(String filePath) throws FileNotFoundException {
		filePath = filePath.replace('\\', '/');
		
		if (exists(filePath)) {
			return filePath;
		}
		if (filePath.startsWith(TestRankConstants.SOURCE_DIR)) {
			String altPath = TestRankConstants.TEST_DIR + filePath.substring(TestRankConstants.SOURCE_DIR.length());
			if (exists(altPath)) {
				return altPath;
			}
		}
		else if (filePath.startsWith(TestRankConstants.TEST_DIR)) {
			String altPath = TestRankConstants.SOURCE_DIR + filePath.substring(TestRankConstants.TEST_DIR.length());
			if (exists(altPath)) {
				return altPath;
			}
		}

		throw new FileNotFoundException(filePath);
	}
	
	private static boolean exists(String filePath) {
		File file = new File(filePath);
		return file.exists();
	}

	// run this test from log4j's tests working directory (e.g., E:\Dev\workspace\log4j\tests)
	public static void main(String[] args) throws FileNotFoundException {
		System.out.println(fix("..\\src\\main\\java\\org\\apache\\log4j\\TTCCLayoutTest.java"));
	}
}
